/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools.dialog;

import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerImage;
import embeddedpaint.model.layers.LayerManager;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FrameRotatePicture extends ToolFrame
{
    private LayerManager        m_imageManager;
    private JFrame              m_application;
    private JSpinner            m_txtDegree         = new JSpinner();
    private JButton             m_buttonOK          = new JButton("OK");
    private JButton             m_buttonCancel      = new JButton("Cancel");
    private Dimension           m_dimension         = new Dimension(250,150);
    private JRadioButton        m_turnRight         = new JRadioButton("Turn right", false);
    private JRadioButton        m_turnLeft          = new JRadioButton("Turn Left", false);
    private ButtonGroup         m_group             = new ButtonGroup();
    private HashMap             m_oldValues;
      
    public FrameRotatePicture (LayerManager imageManager, JFrame application)
    {
        this.m_application      = application;
        this.m_imageManager     = imageManager;
        this.m_oldValues        = new HashMap();
        
        this.setTitle("Tool rotate layer");
        this.setVisible(true);
        this.setEnabled(true);
        this.setSize(m_dimension);
        this.setAlwaysOnTop(true);
        this.setAutoRequestFocus(true);
        this.setResizable(false);
        this.setVisible(rootPaneCheckingEnabled);
        this.setLayout(new GridLayout(3,2));
        
        this.add(m_turnLeft);
        this.add(m_turnRight);
        
        this.add(new Label("Degrees: "));
        
        this.add(m_txtDegree);
        this.add(m_buttonOK);
        this.add(m_buttonCancel);
        
        this.initValues();  
        
        m_buttonCancel.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
               setOldValues();
               close();
            }
        });
        
        m_buttonOK.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                updateValues();
                close();
            }
        });
        
        m_txtDegree.addChangeListener(new ChangeListener() 
        {
            @Override
            public void stateChanged (ChangeEvent e)
            {
                updateValues();
            }
        });

        ActionListener onLeft = new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                m_turnRight.setSelected(false);
                m_turnLeft.setSelected(true);
                
                updateValues();
            }
        };
                
        ActionListener onRight = new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                m_turnRight.setSelected(true);
                m_turnLeft.setSelected(false);
                updateValues();
            }
        };
        
        m_turnRight.addActionListener (onRight);
        m_turnLeft.addActionListener(onLeft);
    }
    
    private void initValues ()
    {
        m_txtDegree.setModel(new SpinnerNumberModel(0, 0, 360, 1));
        saveOldValues();
    }

    private void updateValues ()
    {
        double degree = (int) m_txtDegree.getModel().getValue();
        
        if (m_turnRight.isSelected())
        {
            degree = -degree;
        }
        
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();
        
        for (ALayer img : selectedImages)
        {
              img.setM_rotate(degree);
        }
    }
    
    private void setOldValues ()
     {
         ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();
         
         for (ALayer img : selectedImages)
         {   
              LayerImage imageLayer = (LayerImage) img;
              img.setM_rotate((double)m_oldValues.get(img));
         }
     }

    private void saveOldValues () 
    {
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();
        
        for (ALayer img : selectedImages)
        {   
              LayerImage imageLayer = (LayerImage) img;
              m_oldValues.put(img, img.getM_rotate());
        }
    }
    
    public void close ()
    {
        this.setEnabled(true);
        this.dispose();
        this.updateObserver();
    }  
    
    public void askApplyAndChange ()
    {
        int confirm = JOptionPane.showConfirmDialog(this, "Do you want apply changes ?", "Apply Changes", JOptionPane.YES_NO_OPTION);
        
        if(confirm == JOptionPane.YES_OPTION)
        {
            this.close();
        }
        else
        {
            this.setOldValues();
            this.close();
        }
    } 
    
    public void setDegrees (double d)
    {
        this.m_txtDegree.setValue(Integer.parseInt(""+(int)Math.abs(d)));
    }
}
